﻿
using UnityEngine;

namespace NL.Advanced
{
    public static class CamraExtension
    {

        public static bool ISInView(this Camera who, Vector3 pos)
        {
            return _IsPointInCamera(who, pos);
        }




        public static bool ISInView(this Camera who, Transform where, Bounds bound)
        {
            if (!_IsPointInCamera(who, where.transform.position + bound.min))
                return false;
            if (!_IsPointInCamera(who, where.transform.position + bound.max))
                return false;

            return true;
        }

        public static bool ISInView(this Camera who, Transform where, params Bounds[] bounds)
        {
            foreach (var bb in bounds)
            {
                if (!who.ISInView(where, bb))
                    return false;
            }

            return true;
        }


        static bool _IsPointInCamera(Camera who, Vector3 pos)
        {
            Vector3 vp = who.WorldToViewportPoint(pos);

            return vp.x > 0 && vp.x < 1 && vp.y > 0 && vp.y < 1 && vp.z > 0;

        }
    }
}
